package StreamStudy.Exa02;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 获取不同流的多种方式，下面介绍的只是一部分，关于如何获取流，我们应该灵活应用
 */
public class CreatingStreams {
    /**
     * 此方法用于遍历流中元素，对于无限流，只遍历前10个
     * @param title 流名称
     * @param stream 流对象
     * @param <T> 类型变量
     */
    public static <T> void show(String title, Stream<T> stream){
        final int SIZE = 10;
        List<T> firstElements=stream.limit(SIZE+1).collect(Collectors.toList());
        System.out.print(title+" : ");
        for(int i=0;i<firstElements.size();i++){
            if(i>0) System.out.print(",");
            if(i<SIZE) System.out.print(firstElements.get(i));
            else System.out.print("...");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        String content="hello world, wo cao ni ma";
        String[] contents=content.split("\\PL+");

        //通过Stream.of（）方法获得流，入参是数组
        Stream<String> words = Stream.of(contents);
        show("words",words);

        //通过Stream.of()方法获取流，入参是可变参数
        Stream<String> song=Stream.of("gentley","down","the","stream");
        show("song",song);

        //创建一个空流
        Stream<String> silence=Stream.empty();
        show("silence",silence);

        //获取无限流
        Stream<String> echos=Stream.generate(()->"Echo");
        show("echos",echos);

        //获取无限随机数流
        Stream<Double> randoms=Stream.generate(Math::random);
        show("randoms",randoms);

        //获取叠加无限流
        Stream<BigInteger> integers=Stream.iterate(BigInteger.ONE,n->n.add(BigInteger.ONE));
        show("integers",integers);

        //使用相关API获取流
        Stream<String> wordsAnotherWay = Pattern.compile("\\PL+").splitAsStream(content);
        show("wordsAnotherWay",wordsAnotherWay);

        //
        List<String> stringList = new ArrayList<>();
        Stream<String> stream = stringList.stream();


    }

}
